package com.sicnu.carrent.serivce.impl;

import com.sicnu.carrent.domain.Cars;
import com.sicnu.carrent.repository.CarsRepository;
import com.sicnu.carrent.serivce.CarsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;

@Service
public class CarsServiceImpl implements CarsService {

    @Autowired
    private CarsRepository carsRepository;

    @Override
    public List<Cars> findAllCars() {
        return carsRepository.findAll();
    }

    @Override
    public Page<Cars> findPagerCars(int currentPageNumber, int pageSize) {

        Pageable pager = PageRequest.of(currentPageNumber,pageSize);

        return carsRepository.findAll(pager);
    }

    @Transactional
    @Override
    public void save(Cars c) {
        carsRepository.save(c);
    }

    @Transactional
    @Override
    public void delete(int id) {
        carsRepository.deleteById(id);
    }


    @Override
    public List<String> findBrandsByTypeName(String typeName) {
        return carsRepository.findBrandsByTypeName(typeName);
    }

    @Override
    public List<Cars> findAllByTypeAndBrand(String type, String brand) {
        return carsRepository.findAllByTypeAndBrand(type,brand);
    }
}
